<?php
/*
 * Created on Feb 11, 2011
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */

include_once("../configure/admin.config.inc.php");	//--> admin global var
include_once("db.inc.php");							//--> db global var
include_once("Common.php");							//--> Common

$objCommon	= new Common();			//--> Common

if(isset($_POST['Submit'])){
//	$id					= $_POST['id'];
	$merchantName		= $_POST['merchantName'];
	$categoryID			= $_POST['categoryID'];
	
	$startDate 			= $_POST['startDate'];
	$expireDate			= $_POST['expireDate'];
	$startPrice 		= $_POST['startPrice'];
	$code 				= addslashes($_POST['code']);
	$amount 			= addslashes($_POST['amount']);
	$url 				= addslashes($_POST['url']);
	$longRestr			= addslashes($_POST['longRestr']);

	$comments 			= addslashes($_POST['comments']);
	$descript 			= addslashes($_POST['descript']);
		
	$isFeatured 		= $_POST['isFeatured'];
	$isFreeShipping 	= $_POST['isFreeShipping'];
	$isFreeGift 		= $_POST['isFreeGift'];
	$isClearanceSales 	= $_POST['isClearanceSales'];
	$isFreeSample 		= $_POST['isFreeSample'];
	
	$IDkey 				= $_POST['IDkey'];
	
	$status 			= $_POST['status'];
	
	$merchantID = $objCommon->getColumn(DB_TAG_COUPON, "SELECT id FROM merchant WHERE name = '$merchantName' OR alias = '$merchantName'");
	
	
	if(empty($code) && empty($amount) && empty($descript)){
		$error_message = 'Code or Amount or Descript should\'t be empty.';
	}else if(empty($merchantID)){
		$error_message = $merchantName . ' merchant is not existed.';
	}else if(empty($categoryID)){
		$categoryID = $objCommon->getColumn(DB_TAG_COUPON, "SELECT id FROM category c, mer_category mc WHERE c.id = mc.categoryID AND c.status = 1 AND mc.merchantID = $merchantID LIMIT 1");
		
		if(empty($categoryID)){
			$error_message = $merchantID . ' merchant ID is not existed category.';
		}
	}
	
	
	if(empty($error_message)){
		$sql = "INSERT INTO coupon SET merchantID='$merchantID', categoryID='$categoryID', code = '$code', descript = '$descript', URL = '$url', expireDate = '$expireDate', " .
				"startDate = '$startDate', startPrice = '$startPrice', longRestr = '$longRestr', amount = '$amount', isFeatured = '$isFeatured', " .
				"isFreeShipping = '$isFreeShipping', isFreeGift = '$isFreeGift', isFreeSample = '$isFreeSample', isClearanceSales = '$isClearanceSales', " .
				"status = '$status', comments = '$comments', IDkey = '$IDkey', createdTime = UTC_TIMESTAMP() ON DUPLICATE KEY UPDATE IDkey = VALUES(IDkey)";
		
		$isReturn = $objCommon->update(DB_TAG_COUPON, $sql);
		if($isReturn){
			$error_message = 'add successfully.';
		}else 
			$error_message = 'add failure.';
	}
}else{
	$error_message = 'invalid submit.';
}

echo $error_message;

?>
